package com.lu.pojo.network.tcp.upload;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author luzhenfang
 * @date 2020 05 17
 * @description TCP传输文件 服务端
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        Socket socket = serverSocket.accept();// 阻塞连接 等待
        // 获取输入流
        InputStream is = socket.getInputStream();
        // 文件输出
        FileOutputStream fos = new FileOutputStream(new File("reserve.jpg"));
        byte[] buffer = new byte[1024];
        int len;
        while ((len=is.read(buffer))!=-1){
            fos.write(buffer,0,len);
        }
        // 通知客户端 接收完毕
        OutputStream os = socket.getOutputStream();
        os.write("服务端已接收完毕！".getBytes());


        os.close();
        fos.close();
        is.close();
        socket.close();
    }
}
